package com.yanqu.road.entity.union.parergon;

import com.yanqu.road.entity.DataObject;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class UnionParergon extends DataObject {
    private String unionId;

    private int parergonId;

    private int progress;

    private int parergonTimes;

    private List<Long> playerIds;

    private long dayRestoreTime;

    public String getUnionId() {
        return unionId;
    }

    public void setUnionId(String unionId) {
        if(!Objects.equals(this.unionId, unionId)) {
            this.unionId = unionId;
            setUpdateOption();
        }
    }

    public int getParergonId() {
        return parergonId;
    }

    public void setParergonId(int parergonId) {
        if(this.parergonId != parergonId) {
            this.parergonId = parergonId;
            setUpdateOption();
        }
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        if(this.progress != progress) {
            this.progress = progress;
            setUpdateOption();
        }
    }


    public int getParergonTimes() {
        return parergonTimes;
    }

    public void setParergonTimes(int parergonTimes) {
        if(this.parergonTimes != parergonTimes) {
            this.parergonTimes = parergonTimes;
            setUpdateOption();
        }
    }

    public String getPlayerIds() {
        if (playerIds == null || playerIds.isEmpty()) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        for (Long playerId : playerIds) {
            sb.append(playerId).append(",");
        }
        sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }

    public void setPlayerIds(String playerIds) {
        this.playerIds = new ArrayList<>();
        if (!StringUtils.isNullOrEmpty(playerIds)) {
            for (String playerIdStr : playerIds.split(",")) {
                Long playerId = Long.valueOf(playerIdStr);
                this.playerIds.add(playerId);
            }
        }
        setUpdateOption();
    }

    public boolean checkPlayerId(long playerId, int size) {
        if (size < 1) {
            return false;
        }
        if (playerIds == null) {
            playerIds = new ArrayList<>();
        }
        if (playerIds.contains(playerId)) {
            return true;
        }

        if (playerIds.size() < size) {
            playerIds.add(playerId);
            setUpdateOption();
            return true;
        }

        return false;
    }

    public long getDayRestoreTime() {
        return dayRestoreTime;
    }

    public void setDayRestoreTime(long dayRestoreTime) {
        if(this.dayRestoreTime != dayRestoreTime) {
            this.dayRestoreTime = dayRestoreTime;
            setUpdateOption();
        }
    }
}
